package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.health.common.PageParam;
import com.itheima.health.common.ResultPageData;
import com.itheima.health.entity.TCheckgroup;
import com.itheima.health.entity.TCheckgroupCheckitem;
import com.itheima.health.mapper.TCheckgroupMapper;
import com.itheima.health.model.pojos.CheckGroup;
import com.itheima.health.model.pojos.CheckgroupCheckitem;
import com.itheima.health.pojo.dto.TCheckgroupDTO;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.service.ITCheckgroupCheckitemService;
import com.itheima.health.service.ITCheckgroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cpanda
 * @since 2024-01-25
 */
@Service
public class TCheckgroupServiceImpl extends ServiceImpl<TCheckgroupMapper, TCheckgroup> implements ITCheckgroupService {
    @Autowired
    ITCheckgroupCheckitemService checkgroupCheckitemService;
    @Autowired
    CheckItemService checkItemService;
    /**
     * 检查组查询
     *
     * @param pageParam
     * @return
     */
    @Override
    public ResultPageData cGPpage(PageParam pageParam) {
        Page<TCheckgroup> page = new Page<>(pageParam.getCurrentPage(), pageParam.getPageSize());
        LambdaQueryWrapper<TCheckgroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(pageParam.getQueryString()!=null,TCheckgroup::getHelpCode,pageParam.getQueryString());
        Page<TCheckgroup> checkgroupPage = this.page(page, wrapper);
        List<TCheckgroup> records = checkgroupPage.getRecords();
        ResultPageData pageVO = new ResultPageData();
        pageVO.setTotal(checkgroupPage.getTotal());
        pageVO.setRows(records);
        return pageVO;
    }

    @Override
    public void add(TCheckgroup checkGroup, Integer[] checkitemIds) {
        this.save(checkGroup);
        for (Integer checkitemId : checkitemIds) {
            TCheckgroupCheckitem tCheckgroupCheckitem = new TCheckgroupCheckitem();
            tCheckgroupCheckitem.setCheckgroupId(checkGroup.getId());
            tCheckgroupCheckitem.setCheckitemId(checkitemId);
            checkgroupCheckitemService.save(tCheckgroupCheckitem);
        }

    }

    @Override
    public TCheckgroup checkgroupById(Long id) {
        return this.getById(id);
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer checkgroupId) {
        List<TCheckgroupCheckitem> list = checkgroupCheckitemService.list();
        List<Integer> checkitemList = list.stream().filter(c -> Objects.equals(c.getCheckgroupId(),checkgroupId)).map(c -> c.getCheckitemId()).collect(Collectors.toList());
        return checkitemList;
    }
    /**
     * 更新
     */
    @Override
    public void updateCheckgroup(TCheckgroup dto, Integer[] checkitemIds) {
        //构造条件
        LambdaQueryWrapper <TCheckgroupCheckitem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TCheckgroupCheckitem::getCheckgroupId,dto.getId());
        //先删除原来所有 checkgroupId所对应的checkitemId
        checkgroupCheckitemService.remove(queryWrapper);
        //遍历新增
        for (Integer checkitemId : checkitemIds) {
            TCheckgroupCheckitem checkgroupCheckitem = new TCheckgroupCheckitem();
            checkgroupCheckitem.setCheckgroupId(dto.getId());
            checkgroupCheckitem.setCheckitemId(checkitemId);
            checkgroupCheckitemService.save(checkgroupCheckitem);
        }
        //修改
        this.updateById(dto);

    }

    @Override
    public void checkgroupDelete(Integer id) {
        LambdaQueryWrapper<TCheckgroupCheckitem> queryWrapper= new LambdaQueryWrapper<>();
        queryWrapper.eq(TCheckgroupCheckitem::getCheckgroupId,id);
        checkgroupCheckitemService.remove(queryWrapper);
        this.removeById(id);
    }
}
